package regelinterpreter02;

import java.util.ArrayList;
import java.util.List;

public class Logic {
    private List<Module> serialModules = new ArrayList<Module>();
    private List<String> diagnosis = new ArrayList<String>();
    private List<String> answers = new ArrayList<String>();
    private String jLabel_Module = "";
    private String jLabel_Image = "";
    private String jLabel_Question = "";
    private String jLabel_Answer2 = "";
    private Rule globalRule = new Rule();
    private Tools tools = new Tools();
    private int i1 = 0;
    private int j1 = 0;
    private int j2 = 0;
    private int j3 = 0;
    private int statementCounter = 0;
    private int ind = -1;
    private boolean jButton_next_setVisible = true;
    private boolean jButton_finished_setVisible = true;
    
    public void serializeTreeToList(List<Module> modules){      //serialisieren des durch den Parser erzeugten Objektbaumes
        if(modules.size() > 0){                                 //Speicherung in Liste "serialModules" fuer Weiterverarbeitung
            for(int i = 0; i < modules.size(); i++){
                statementCounter += modules.get(i).getRules().size();
                for(int j = 0; j<modules.get(i).getConsistsOfParts().size(); j++){
                    statementCounter += modules.get(i).getConsistsOfParts().get(j).getRules().size();
                }
                serialModules.add(modules.get(i));
                if(modules.get(i).getConsistsOfModules().size() > 0){
                    serializeTreeToList(modules.get(i).getConsistsOfModules());
                }
            }
        }
    }
    
    public boolean doNext(){            //ausgeloest durch den "Weiter"-Button, ruft solange logicModule() auf, bis neue globalRule geladen
        Rule rTemp = globalRule;        //ueberspringt so Elemente ohne eigene Regel
        if(statementCounter != 0){
            do{
                logicModule();
            } while (globalRule.equals(rTemp) && statementCounter > 0);
        } else {                        //setzen der Ende-Anzeige falls keine weiteren Aussagen vorhanden (statementCounter=0)
            jButton_next_setVisible = false;
            jLabel_Module = "Ende";
            jLabel_Image= "pictures/ende.jpg";
            diagnosis.clear();
            answers.clear();
        }
        return true;
    }
    
    public void logicModule(){      //durchlaufen der serialModules-Liste und Bereitstellen der Labelnamen 
        if(i1 >= serialModules.size()){
            jLabel_Module = "Ende";
            jLabel_Image= "pictures/ende.jpg";
        } else {
            if(serialModules.size() > 0 && i1 < serialModules.size()){
                jLabel_Module = serialModules.get(i1).getName();
                //Rules des Modules in globale Regel für Weiterverarbeitung schreiben 
                if(serialModules.get(i1).getRules().size() > 0 && j1 < serialModules.get(i1).getRules().size()){
                    globalRule = serialModules.get(i1).getRules().get(j1);  //weitergabe der Rules des Modules
                    logicRule();
                    j1++;
                 
                } 
                //Rules der Parts des Modules auswerten
                else if(serialModules.get(i1).getConsistsOfParts().size() > 0 && j2 < serialModules.get(i1).getConsistsOfParts().size()){
                    if(serialModules.get(i1).getConsistsOfParts().get(j2).getRules().size() > 0 && j3 < serialModules.get(i1).getConsistsOfParts().get(j2).getRules().size()){
                        globalRule = serialModules.get(i1).getConsistsOfParts().get(j2).getRules().get(j3); //weitergabe der Rules der Parts des Modules
                        jLabel_Module = serialModules.get(i1).getConsistsOfParts().get(j2).getName();
                        logicRule();
                        j3++;
                    }else{
                        j2++;
                    }
                //nach Auswertung der Rules und Parts wird i1++ um zum naechsten Module zu kommen
                } else {    //ruecksetzen der globalen Zaehler falls ein Module-Objekt fertig abgearbeitet
                    i1++;
                    j2 = 0;
                    j3 = 0;
                    j1 = 0;
                    if(i1 >= serialModules.size()){
                        jLabel_Module = "Ende";
                        jLabel_Image = "pictures/ende.jpg";
                    }
                }
            }
        }
    }
	
    public void logicPart(List<Part> parts){    //zur Zeit nicht verwendet
        //Regelauswertung f�r Regeln der Parts
        if(parts.size() > 0){
            for(int i=0; i<parts.size(); i++){
                if(parts.get(i).getRules().size() > 0){
                    for(int j=0; j<parts.get(i).getRules().size(); j++){
                        globalRule = parts.get(i).getRules().get(j);
                    }
                }
            }
        }
    }
	
    public void logicRule(){    //Weitergabe der Aussage der aktuellen Regel an die createQuestion()-Methode
        createQuestion(globalRule.getStatement());
    }
        
    public void createQuestion(Statement statement){    //Frage, Werte und Diagnosen der Oberflaeche zur Verfügung stellen
        if(statement.getQuestion() != null){
            jLabel_Question = statement.getQuestion();
            diagnosis.clear();
            answers.clear();
            if(statement.getPicture() != null) jLabel_Image = "pictures/"+statement.getPicture();
            //falls Wert mit mehreren Elementen (bool, String-Liste)
            if(statement.getValue_string().size() > 1){
                for(int i = 0; i < statement.getValue_string().size(); i++){
                    answers.add(statement.getValue_string().get(i));
                    diagnosis.add(statement.getDiagnosis().get(i));
                }	
            }
            statementCounter--;     //dekrementieren des Aussagenzaehlers
        }
        
    }
    
    public void startFunction(){    //ruecksetzen der globalen Zaehlvariablen und der Diaogkomponentenelemente
        // Wissenbasis serialisieren und Fortschritt zuruecksetzen
        serialModules.clear();
        Parser parser = new Parser();
        serializeTreeToList(parser.csvParsen("Wissensbasis.csv"));  //starten des Parsers und serialisieren seines Ergebnisses
        diagnosis.clear();
        answers.clear();
        i1 = 0;
        j1 = 0;
        j2 = 0;
        j3 = 0;
        jButton_next_setVisible = true;
        jButton_finished_setVisible = false;
    }

    public boolean useRule(String itemAtIndex){     //feuern der Regel und Bereitstellen der Diagnose
        if(!(itemAtIndex.equals(""))){
            globalRule.getStatement().setChosenValue(itemAtIndex);
            try{
                jLabel_Answer2 = globalRule.startRule();
                if(statementCounter != 0) jButton_next_setVisible = true;
                jButton_finished_setVisible = true;
            }
            catch (Exception e) {
                jLabel_Answer2 = "Diagnose";
            }
        } else {
            jLabel_Answer2 = "Diagnose";
            if(statementCounter != 0) jButton_next_setVisible = true;
            jButton_finished_setVisible = false;
        }
        return true;
    }
    
    /**
     * @return the serialModules
     */
    public List<Module> getSerialModules() {
        return serialModules;
    }

    /**
     * @param serialModules the serialModules to set
     */
    public void setSerialModules(List<Module> serialModules) {
        this.serialModules = serialModules;
    }

    /**
     * @return the globalRule
     */
    public Rule getGlobalRule() {
        return globalRule;
    }

    /**
     * @param globalRule the globalRule to set
     */
    public void setGlobalRule(Rule globalRule) {
        this.globalRule = globalRule;
    }

    /**
     * @return the i1
     */
    public int getI1() {
        return i1;
    }

    /**
     * @param i1 the i1 to set
     */
    public void setI1(int i1) {
        this.i1 = i1;
    }

    /**
     * @return the j1
     */
    public int getJ1() {
        return j1;
    }

    /**
     * @param j1 the j1 to set
     */
    public void setJ1(int j1) {
        this.j1 = j1;
    }

    /**
     * @return the j2
     */
    public int getJ2() {
        return j2;
    }

    /**
     * @param j2 the j2 to set
     */
    public void setJ2(int j2) {
        this.j2 = j2;
    }

    /**
     * @return the j3
     */
    public int getJ3() {
        return j3;
    }

    /**
     * @param j3 the j3 to set
     */
    public void setJ3(int j3) {
        this.j3 = j3;
    }

    /**
     * @return the statementCounter
     */
    public int getStatementCounter() {
        return statementCounter;
    }

    /**
     * @param statementCounter the statementCounter to set
     */
    public void setStatementCounter(int statementCounter) {
        this.statementCounter = statementCounter;
    }

    /**
     * @return the diagnosis
     */
    public List<String> getDiagnosis() {
        return diagnosis;
    }

    /**
     * @param diagnosis the diagnosis to set
     */
    public void setDiagnosis(List<String> diagnosis) {
        this.diagnosis = diagnosis;
    }

    /**
     * @return the answers
     */
    public List<String> getAnswers() {
        return answers;
    }

    /**
     * @param answers the answers to set
     */
    public void setAnswers(List<String> answers) {
        this.answers = answers;
    }

    /**
     * @return the ind
     */
    public int getInd() {
        return ind;
    }

    /**
     * @param ind the ind to set
     */
    public void setInd(int ind) {
        this.ind = ind;
    }

    /**
     * @return the tools
     */
    public Tools getTools() {
        return tools;
    }

    /**
     * @param tools the tools to set
     */
    public void setTools(Tools tools) {
        this.tools = tools;
    }

    /**
     * @return the jLabel_Module
     */
    public String getjLabel_Module() {
        return jLabel_Module;
    }

    /**
     * @param jLabel_Module the jLabel_Module to set
     */
    public void setjLabel_Module(String jLabel_Module) {
        this.jLabel_Module = jLabel_Module;
    }

    /**
     * @return the jLabel_Image
     */
    public String getjLabel_Image() {
        return jLabel_Image;
    }

    /**
     * @param jLabel_Image the jLabel_Image to set
     */
    public void setjLabel_Image(String jLabel_Image) {
        this.jLabel_Image = jLabel_Image;
    }

    /**
     * @return the jLabel_Question
     */
    public String getjLabel_Question() {
        return jLabel_Question;
    }

    /**
     * @param jLabel_Question the jLabel_Question to set
     */
    public void setjLabel_Question(String jLabel_Question) {
        this.jLabel_Question = jLabel_Question;
    }
    
    public String getjLabel_Answer2() {
        return jLabel_Answer2;
    }

    public void setjLabel_Answer2(String jLabel_Answer2) {
        this.jLabel_Answer2 = jLabel_Answer2;
    }  
    
    public void setjButton_next_setVisible(boolean b){
        this.jButton_next_setVisible = b;
    }
    
    public boolean isjButton_next_setVisible(){
        return jButton_next_setVisible;
    }
    
    public void setjButton_finished_setVisible(boolean b){
        this.jButton_finished_setVisible = b;
    }
    
    public boolean jButton_finished_setVisible(){
        return jButton_finished_setVisible;
    }
}